/*
 * Copyright (c) 2024-present HBIS Digital Technology Co.,Ltd. All rights reserved.
 */
package com.hbisdt.cet.sample.service.impl;

import com.hbisdt.cet.sample.entity.ResponseData;
import com.hbisdt.cet.sample.entity.dto.CarbonArticleRequest;
import com.hbisdt.cet.sample.entity.po.CarbonArticle;
import com.hbisdt.cet.sample.mapper.CarbonArticleMapper;
import com.hbisdt.cet.sample.service.ICarbonArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 碳业务文章表 服务实现类
 *
 * @author : 教学示例
 * @version : 1.0
 * @date : 2024-12-10 11:54
 */
@Service
public class CarbonArticleServiceImpl implements ICarbonArticleService {

    @Autowired
    private CarbonArticleMapper carbonArticleMapper;

    /**
     * 增加碳业务文章
     *
     * @param request 请求实体
     * @return com.hbisdt.cet.sample.entity.ResponseData<?>
     * @author : 教学示例
     */
    @Override
    public ResponseData<?> addCarbonArticle(CarbonArticleRequest request) {
        CarbonArticle article = new CarbonArticle();
        article.setTitle(request.getTitle());
        article.setDescription(request.getDescription());
        article.setContent(request.getContent());
        article.setPublish(true);
        article.setDelFlag(false);
        carbonArticleMapper.insert(article);
        return ResponseData.ok();
    }

    /**
     * 修改碳业务文章
     *
     * @param request 请求实体
     * @return com.hbisdt.cet.sample.entity.ResponseData<?>
     * @author : 教学示例
     */
    @Override
    public ResponseData<?> editCarbonArticle(CarbonArticleRequest request) {
        CarbonArticle article = carbonArticleMapper.selectById(request.getArticleId());
        article.setTitle(request.getTitle());
        article.setDescription(request.getDescription());
        article.setContent(request.getContent());
        article.setUpdateTime(new Date());
        carbonArticleMapper.updateById(article);
        return ResponseData.ok();
    }

    /**
     * 删除碳业务文章
     *
     * @param request 请求实体
     * @return com.hbisdt.cet.sample.entity.ResponseData<?>
     * @author : 教学示例
     */
    @Override
    public ResponseData<?> deleteCarbonArticle(CarbonArticleRequest request) {
        carbonArticleMapper.deleteById(request.getArticleId());
        return ResponseData.ok();
    }

    /**
     * 碳业务文章列表
     *
     * @return java.util.List<com.hbisdt.cet.sample.entity.po.CarbonArticle>
     * @author : 教学示例
     */
    @Override
    public List<CarbonArticle> getCarbonArticleList() {
        return carbonArticleMapper.selectAll(null);
    }

    /**
     * 根据id获取碳业务文章
     *
     * @return com.hbisdt.cet.sample.entity.po.CarbonArticle
     * @author : 教学示例
     */
    @Override
    public CarbonArticle getById(Long articleId) {
        return carbonArticleMapper.selectById(articleId);
    }

}
